{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# How to debug in BrainPy\n",
    "\n",
    "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/brainpy/brainpy/blob/master/docs_version2/tutorial_FAQs/how_to_debug.ipynb)\n",
    "[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/brainpy/brainpy/blob/master/docs_version2/tutorial_FAQs/how_to_debug.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-10-06T03:58:59.235219Z",
     "start_time": "2025-10-06T03:58:54.913341Z"
    }
   },
   "source": [
    "import jax\n",
    "import brainpy as bp\n",
    "import brainpy.math as bm\n",
    "\n",
    "bm.set_platform('cpu')\n",
    "\n",
    "bp.__version__"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.0.0'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## ``jax.disable_jit()`` context"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "To debug your model on BrainPy, users should turn off the JIT mode by using  ``jax.disable_jit()``."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-10-06T03:58:59.243738Z",
     "start_time": "2025-10-06T03:58:59.239386Z"
    }
   },
   "source": [
    "@bm.jit\n",
    "def f1(a):\n",
    "    print(f'call, a = {a} ...')\n",
    "    return a"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "With JIT mode, the above code will produce:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-10-06T03:58:59.265892Z",
     "start_time": "2025-10-06T03:58:59.248751Z"
    }
   },
   "source": [
    "f1(1.)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "call, a = JitTracer<~float32[]> ...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Array(1., dtype=float32, weak_type=True)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "The first ``call`` is used to infer the dynamical variables (``brainpy.math.Variable``) used in this function. The second ``call`` is used to compile the whole function. Note that, with JIT mode, we cannot get the concrete values in the function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "We can turn off the JIT with ``jax.disable_jit()`` context manager."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-10-06T03:58:59.275879Z",
     "start_time": "2025-10-06T03:58:59.271939Z"
    }
   },
   "source": [
    "with jax.disable_jit():\n",
    "    f1(1.)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "call, a = 1.0 ...\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "As you can see, the above code prints the concrete value used in the model. In such a way, ones can integrate standard debugging tools in your model design."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "``jax.disable_jit()`` works for most brainpy transformations, including:\n",
    "\n",
    "- ``brainpy.math.jit()``\n",
    "- ``brainpy.math.grad()``\n",
    "- ``brainpy.math.vector_grad()``\n",
    "- ``brainpy.math.while_loop()``\n",
    "- ``brainpy.math.cond()``\n",
    "- ``brainpy.math.ifelse()``"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## ``brainpy.DSRunner(..., jit=False)``\n",
    "\n",
    "If users are using ``brainpy.DSRunner``, you can initialize ``brainpy.DSRunner(..., jit=False)`` to disable JIT compilation when simulating a brain dynamics model.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## ``brainpy.for_loop(..., jit=False)``\n",
    "\n",
    "Similarly, if users are using ``brainpy.for_loop``, you can put a ``jit=False`` argument into the ``for_loop`` transformation, then the JIT compilation will be removed."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
